﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.Utils;
using CollegeShop.Repository.Win.CSWS;
using CollegeShop.Api.Framework;
using CollegeShop.Repository.Win.ViewObject;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using CollegeShop.Repository.Win.DevHelper;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.IO;
namespace CollegeShop.Repository.Win.BaseData
{
    public partial class F_Goods : F_Base
    {
        private int page = 0;
        private int pageSize = 50;
        private bool m_checkStatus = false;
        private PictureEdit _pedDelete; //删除
        public F_Goods()
        {           
            InitializeComponent();

            this.gv_data.IndicatorWidth = 40;
            gv_data.Click += new EventHandler(gv_data_Click);
            gv_data.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(gv_data_CustomDrawColumnHeader);
            
            InitCategory();

            for (int i = 0; i < gv_data.Columns.Count; i++)
            {
                gv_data.Columns[i].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            }

        }

        #region 数据初始化
        /// <summary>
        /// 初始化商品数据
        /// </summary>
        /// <param name="categoryId"></param>
        private void InitGoods(int categoryId,string categoryName) {
            List<CommodityVO> commodityVOs = TypePlug.ToObject<List<CommodityVO>>( new CollegeShopAPISoapClient().GetCommodities(Program.college,categoryId, page, pageSize));
            this.gv_data.GroupPanelText = categoryName + " 类别下的商品：";
            this.gc_data.DataSource = commodityVOs;
        }
        /// <summary>
        /// 初始化商品类型数据
        /// </summary>
        private void InitCategory()
        {
            List<CommodityTypeVO> commodityTypeVOs = new List<CommodityTypeVO>();
            commodityTypeVOs = TypePlug.ToObject<List<CommodityTypeVO>>(new CollegeShopAPISoapClient().GetCommodityType(Program.college));
            
            if (commodityTypeVOs != null && commodityTypeVOs.Count != 0)
            {
                this.tl_goodsCatetory.DataSource = commodityTypeVOs;
                this.tl_goodsCatetory.Columns[0].Visible = false;
                this.tl_goodsCatetory.Columns[1].Caption = "类别名称";
                this.tl_goodsCatetory.Columns[1].OptionsColumn.ReadOnly = true;
                this.tl_goodsCatetory.Columns[1].OptionsColumn.AllowEdit = false;
                this.tl_goodsCatetory.Columns[1].OptionsColumn.AllowSort = false;
                this.tl_goodsCatetory.Columns[1].OptionsColumn.AllowMove = false;
            }
        }

        
        #endregion

        private void gc_data_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Append) {
                AddGoods();
            }
            else if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                DelGoods();
            }
            else if (e.Button.ButtonType == NavigatorButtonType.Edit)
            {
                EditGoods();
            }
            else if (e.Button.ButtonType == NavigatorButtonType.First)
            {

            }
            else if (e.Button.ButtonType == NavigatorButtonType.Last)
            {

            }
            else if (e.Button.ButtonType == NavigatorButtonType.NextPage)
            {

            }
            else if (e.Button.ButtonType == NavigatorButtonType.PrevPage)
            {

            }
        }

        #region 增删改
        private void AddGoods() {
            
        }
        private void EditGoods() { 
        }
        private void DelGoods() { 
        }

        #endregion 

        #region 分页操作
        private void FirstPage()
        {
            page = 0;
            
        }
        private void LastPage()
        {
        }
        private void NextPage()
        {
        }
        private void PrevPage()
        {
        }
        #endregion

        private void sb_addGoods_Click(object sender, EventArgs e)
        {
            F_GoodsInput goodsInput = new F_GoodsInput();
            goodsInput.ShowDialog();
        }

        private void sb_importExcel_Click(object sender, EventArgs e)
        {
            F_ImportExcel importExcel = new F_ImportExcel();
            importExcel.ShowDialog();
        }

        private void tl_goodsCatetory_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            TreeListNode tl = this.tl_goodsCatetory.FocusedNode;
    
            if (tl != null) {
                int categoryId = Convert.ToInt32(tl.GetValue("Id"));
                InitGoods(categoryId, tl.GetDisplayText("Name"));
            }
        }

        private void tl_goodsCatetory_NodeCellStyle(object sender, DevExpress.XtraTreeList.GetCustomNodeCellStyleEventArgs e)
        {
            if (e.Node.Selected)
            {
                e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
                e.Appearance.ForeColor = Color.Blue; e.Appearance.BorderColor = Color.Red;
            }
        }

        private void gv_data_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Images" && e.IsGetData)
            {

                GridView view = sender as GridView;

                string imgUrl = (string)((DataRowView)e.Row)["Images"];


                Image img = null;

                try
                {
                    img = Image.FromFile(imgUrl);

                }

                catch
                {

                }


                e.Value = img;

            }
        }

        private void gv_data_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gv_data_Click(object sender, EventArgs e)
        {
            if (DevControlHelper.ClickGridCheckBox(this.gv_data, "check", m_checkStatus))
            {
                m_checkStatus = !m_checkStatus;
            }
        }

        private void gv_data_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName.Equals("check"))
            {
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DevControlHelper.DrawCheckBox(e, m_checkStatus);
                e.Handled = true;
            }
        }

        private void gv_data_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName.Equals("State"))
            {
                int state = Convert.ToInt32(e.Value);
                switch (state)
                {
                    case 0: e.DisplayText = "未销售"; break;
                    case 1: e.DisplayText = "已销售"; break;
                    default: e.DisplayText = "未销售"; break;
                }
            }
        }

        #region 小图标删除点击事件
        private void gv_data_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo htInfo = view.CalcHitInfo(e.X, e.Y);
            if (!htInfo.InRowCell)
            {

                if (_pedDelete != null)
                    _pedDelete.Visible = false;
                return;
            }
            string name = htInfo.Column.FieldName; //取得鼠标所在的列名
            //this.gv_data.FocusedRowHandle = htInfo.RowHandle;
            int row = this.gv_data.FocusedRowHandle;
            if (row < 0)
                return;

            if ("Code".Equals(name) && htInfo.RowHandle >= 0) //判断是那一列中显示 删除图标
            {
                GridViewInfo vInfo = view.GetViewInfo() as GridViewInfo;
                GridCellInfo cInfo = vInfo.GetGridCellInfo(htInfo);
                if (_pedDelete == null)
                {
                    _pedDelete = new PictureEdit(); //实例删除图片按钮
                    Stream stream = DevControlHelper.GetImgStream("delete.ico"); //加载图片     加载图片函数在最后。
                    if (stream != null)
                        _pedDelete.Image = System.Drawing.Image.FromStream(stream);
                    _pedDelete.Click += new EventHandler(ImgDel_Click); //注册删除事件   具体实践如下
                    gc_data.Controls.Add(_pedDelete);
                }

                Rectangle r = new Rectangle(cInfo.Bounds.Right - 16, cInfo.Bounds.Bottom - 15, 15, 15); //调整显示图片位置
                _pedDelete.Bounds = r; // 设定位置
                _pedDelete.Tag = htInfo.RowHandle;
                r.X -= 16;
                _pedDelete.Visible = true;
            }
            else
            {
                if (_pedDelete != null)
                    _pedDelete.Visible = false;
            }
        }
        /// <summary>
        /// 小图标删除点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        protected void ImgDel_Click(object sender, EventArgs e)
        {
            int row = this.gv_data.FocusedRowHandle;
            if (row < 0)
                return;
            if (XtraMessageBox.Show(this, "确定删除该数据嘛？删除后将无法恢复！", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                //检查该乙方是否有招聘人信息
                string htId = gv_data.GetDataRow(row)["合同ID"].ToString();
                int jfId = Convert.ToInt32(gv_data.GetDataRow(row)["甲方ID"]);
                
                
            }
        }
        #endregion

        #region 右键菜单
        /// <summary>
        /// 设置右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gv_data_MouseUp(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi = this.gv_data.CalcHitInfo(e.Location);
            if (hi.InRow && e.Button == MouseButtons.Right)
            {
                int row = this.gv_data.FocusedRowHandle;

                if (row < 0)
                    return;
                
                for (int i = 0; i < barManager.Items.Count; i++)
                {
                    
                }
                pMenu.Manager = barManager;
                pMenu.ShowPopup(Control.MousePosition);
            }
        }
        #endregion

        

    }
}
